package com.muatocroi.tornado;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;

import android.util.Log;

public abstract class CommandSignal {
	
	public static short password;
	public byte cmdCode;
	public byte exCode;
	public int arg = 0;
	
	
	private static final int UDPPort = 15243;
	private static SocketAddress broadcast = null;
	public static final int size = 8;
	


	public static void setBroadcastAddress(InetAddress addr) {
		broadcast = new InetSocketAddress(addr, UDPPort);
	}

	public static SocketAddress getBroadcastAddress() {
		return broadcast;
	}

	public DatagramPacket ToPacket() {
		if (broadcast == null)
			return null;
		ByteBuffer b = ByteBuffer.allocate(size);
		b.putShort(password);
		b.put(cmdCode);
		b.put(exCode);
		b.putInt(arg);
		DatagramPacket p = null;
		try {
			p = new DatagramPacket(b.array(), size, broadcast);
		} catch (SocketException e) {
			Log.i("", e.getMessage());
		}
		return p;
	}
}
